﻿using System;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Design;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Design;
using DatabaseGroup.SSIS.SourceAdapters.View;
using DatabaseGroup.SSIS.SourceAdapters.Model;
using DatabaseGroup.SSIS.SourceAdapters.Presenter;

namespace DatabaseGroup.SSIS.SourceAdapters
{
    /// <summary>
    /// Class to provide an interface to the main UI
    /// for the component
    /// </summary>
    public class DelimitedFileSource2005ComponentUI : IDtsComponentUI
    {
        private IDTSComponentMetaData90 _componentMetadata = null;
        private IServiceProvider _serviceProvider = null;
        private IDtsConnectionService _connService = null;
        private readonly LoadingSplashView _loadingSplash = new LoadingSplashView();

        /// <summary>
        /// Initializes the user interface of the component
        /// </summary>
        /// <param name="dtsComponentMetadata">The underlying component metadata</param>
        /// <param name="serviceProvider">The service provider provided by the SSIS designer</param>
        /// <remarks>
        /// This method is called by SSIS when a component is initially 
        /// added to the SSIS Designer surface, and prior to the New method. 
        /// It is also called when a component is edited, but prior to the Edit method.
        /// </remarks>
        public void Initialize(IDTSComponentMetaData90 dtsComponentMetadata, IServiceProvider serviceProvider)
        {
            ArgumentVerifier.CheckObjectArgument(dtsComponentMetadata, "componentMetadata");
            ArgumentVerifier.CheckObjectArgument(serviceProvider, "serviceProvider");

            _componentMetadata = dtsComponentMetadata;
            _serviceProvider = serviceProvider;

            _connService = (IDtsConnectionService)_serviceProvider.GetService(typeof(IDtsConnectionService));
        }

        #region IDtsComponentUI Members

        /// <summary>
        /// Handles when the component is deleted from the SSIS design surface,
        /// not currently used for anything
        /// </summary>
        /// <param name="parentWindow">The handle of the parent window hosting the component</param>
        public void Delete(IWin32Window parentWindow)
        {
            
        }

        /// <summary>
        /// Loads the user interface for when the component is edited
        /// </summary>
        /// <param name="parentWindow">The handle of the parent window hosting the component</param>
        /// <param name="variables">The collection of variables accessible to the data flow task containing the component</param>
        /// <param name="connections">The collection of connections accessible to the data flow task containing the component</param>
        /// <returns>True if the component has been modified</returns>
        public bool Edit(IWin32Window parentWindow, Variables variables, Connections connections)
        {
            // sometimes when reading in a large set of columns the component can take a few seconds to initialize
            // we stick a small splash screen up there just to assure the user it's coming!
            _loadingSplash.Show(parentWindow);
            System.Windows.Forms.Application.DoEvents();

            try
            {
                EditorView view = new EditorView();
                ComponentModel componentModel = new ComponentModel(_componentMetadata, _connService, connections);
                SourceFileModel fileModel = new SourceFileModel();

                // TODO: comment ...
                new MainPresenter(view);
                new GeneralTabPresenter(view, componentModel);
                new ColumnTabPresenter(view, componentModel, fileModel);
                new AdvanceTabPresenter(view, componentModel);
                new ErrorTabPresenter(view, componentModel);
                new AuditTabPresenter(view, componentModel);
                new PreviewTabPresenter(view, componentModel, fileModel);

                _loadingSplash.Hide();
                System.Windows.Forms.Application.DoEvents();

                if (view.ShowDialog(parentWindow) == DialogResult.OK)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                if (_loadingSplash.Visible)
                    _loadingSplash.Hide();

                // TODO: MessageStrings
                MessageBox.Show(parentWindow, ex.Message + Environment.NewLine + ex.StackTrace,
                    "Exception in Delimited File Source Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
            finally
            {
                if (_loadingSplash.Visible)
                {
                    _loadingSplash.Hide();
                    System.Windows.Forms.Application.DoEvents();
                }
            }
        }

        /// <summary>
        /// For future use in providing help for use of the component,
        /// not currently implemented
        /// </summary>
        /// <param name="parentWindow">The handle of the parent window hosting the component</param>
        public void Help(IWin32Window parentWindow)
        {
            
        }

        /// <summary>
        /// Handles when the component is first added to the SSIS design surface,
        /// not currently implemented
        /// </summary>
        /// <param name="parentWindow">The handle of the parent window hosting the component</param>
        public void New(IWin32Window parentWindow)
        {
            
        }

        #endregion
    }
}